﻿using System.Collections.Generic;
using System.Linq;
using Harvest.Api.Model;
using Harvest.Api.Model.Internal;
using Harvest.Api.Types;

namespace Harvest.Api.ExtensionMethods
{

    public static class ExpenseExtensions
    {

        public static Expense ToExpense(this ExpenseItem entity)
        {
            var result = new Expense
            {
                ExpenseCategoryId = entity.ExpenseCategoryId.HasValue ? entity.ExpenseCategoryId.Value : (int?)null,
                HasReceipt = entity.HasReceipt,
                Id = entity.Id.HasValue ? entity.Id.Value : (int?)null,
                IsBilled = entity.IsBilled,
                IsClosed = entity.IsClosed,
                Notes = entity.Notes,
                ProjectId = entity.ProjectId.HasValue ? entity.ProjectId.Value : (int?)null,
                ReceiptUrl = entity.ReceiptUrl,
                SpentAt = entity.SpentAt,
                TotalCosts = entity.TotalCosts.HasValue ? entity.TotalCosts.Value : (double?)null,
                Units = entity.Units.HasValue ? entity.Units.Value : (double?)null,
                UserId = entity.UserId.HasValue ? entity.UserId.Value : (int?)null
            };
            return result;
        }


        public static ExpenseItem ToExpenseItem(this Expense entity)
        {
            var id = new NullableInt();
            if (entity.Id.HasValue) id = entity.Id.Value;

            var expenseCategoryId = new NullableInt();
            if (entity.ExpenseCategoryId.HasValue) expenseCategoryId = entity.ExpenseCategoryId.Value;

            var projectId = new NullableInt();
            if (entity.ProjectId.HasValue) projectId = entity.ProjectId.Value;

            var userId = new NullableInt();
            if (entity.UserId.HasValue) userId = entity.UserId.Value;

            var totalCosts = new NullableDouble();
            if (entity.TotalCosts.HasValue) totalCosts = entity.TotalCosts.Value;

            var units = new NullableDouble();
            if (entity.Units.HasValue) units = entity.Units.Value;

            var result = new ExpenseItem
            {
                ExpenseCategoryId = expenseCategoryId,
                HasReceipt = entity.HasReceipt,
                Id = id,
                IsBilled = entity.IsBilled,
                IsClosed = entity.IsClosed,
                Notes = entity.Notes,
                ProjectId = projectId,
                ReceiptUrl = entity.ReceiptUrl,
                SpentAt = entity.SpentAt,
                TotalCosts = totalCosts,
                Units = units,
                UserId = userId
            };
            return result;
        }


        public static List<Expense> ToExpenses(this IEnumerable<ExpenseItem> entity)
        {
            var result = entity.Select(x => x.ToExpense()).ToList();
            return result;
        }


        public static List<ExpenseItem> ToExpensesItem(this IEnumerable<Expense> entity)
        {
            var result = entity.Select(x => x.ToExpenseItem()).ToList();
            return result;
        }
    }

}
